package com.stone.business.plan.action;
/**
 * -----计划  ACTION---
 */

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.stone.business.plan.pojo.TGzrwdOld;
import com.stone.business.plan.pojo.TPlan;
import com.stone.business.plan.pojo.TPlanBz;
import com.stone.business.plan.service.GzrwCreatePlanService;
import com.stone.business.plan.service.PlanService;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TBdz;
import com.stone.sys.pojo.TDictionary;
import com.stone.sys.pojo.TOrgan;
import com.stone.utils.DateFormatTool;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;

public class GzrwdCreatePlanAction extends BaseAction{
	
	private GzrwCreatePlanService gzrwCreatePlanService;

	private TGzrwdOld entity;
	
	private String id;
	private String status;
	
	private List<TBdz> bdzList;
	private List<TDictionary> dydjList;
	private List<TDictionary> sblxList;
	private List<TPlanBz> planbzList;
	private String tdwf;
	private String hiddenids;
	private TPlan plan;
	/**
	 * 进入月度工作计划详细页面
	 * 
	 * @return
	 * @throws AppException
	 */
	public String initMonthPlanDetail() throws AppException {
		

		// 修改进入
		if (!"0".equals(id.toString())) {
			 
			this.setSessionAttribute("planid", id);
			this.entity = (TGzrwdOld) gzrwCreatePlanService.findById(Long.parseLong(id),TGzrwdOld.class.getName());
			if (this.entity == null) {
				this.entity = new TGzrwdOld();
				this.entity.setState("0");//起草
			}
			
		// 新建进入
		} else {
			
			this.entity = new TGzrwdOld();
			this.entity.setState("0");//起草
			this.entity.setOuid(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE).toString());
		}
		
		//setDictionary();
		
		return SUCCESS;
		
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws AppException
	 */
	public String saveMonthPlan() throws AppException {

		Date confDate = DateFormatTool.formatToDate(gzrwCreatePlanService.getSysdate("yyyy-MM-dd HH24:mi"), true);
		
	///	String planid = this.getSessionAttribute("planid");
				this.entity.setPlanid(Long.parseLong("0"));
				this.entity.setOuid(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE).toString());
			 
		String rtn = gzrwCreatePlanService.doSave(this.entity, this.hiddenids);
		
	//	setDictionary();
		
		super.setInfo(rtn);

		return SUCCESS;

	}
	
	/**
	 * 删除
	 * 
	 * @return
	 * @throws AppException
	 */
	public String deleteMonthPlan() throws AppException {
		
		gzrwCreatePlanService.doDelAll(id, TPlan.class.getName());
		gzrwCreatePlanService.deleteByDHql("planid in (" + id + ")", null, TPlanBz.class.getName());
		
		return "DELETE_SUCCESS";
	}

	/**
	 * 提交 直接审核
	 * 
	 * @return
	 * @throws AppException
	 */
	public String confirmGzrwd() throws AppException {
		gzrwCreatePlanService.updateByDHql("status = '" + super.getRequest().getParameter("flag") + "'", "id in (" + this.id + ")", null, null, TGzrwdOld.class.getName());
		return "DELETE_SUCCESS";	
	}
	
	/**
	 * 获得字典项目
	 */
	private void setDictionary() {
		
		// 变电站
		this.bdzList = gzrwCreatePlanService.findListByHql(" from TBdz order by sortid");
		// 电压等级
		this.dydjList = gzrwCreatePlanService.findListByHql(" from TDictionary where itemType = 'DYDJ' order by sortid");
		// 电压等级
		this.sblxList = gzrwCreatePlanService.findListByHql(" from TDictionary where itemType = 'SBLX' order by sortid");
		
		TOrgan o  = (TOrgan)super.getSession().getAttribute(IConstant.SESSION_TORGAN);
		String where = (o.getOrganCode().toString().length() <= 6 || "1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) ? "" : " and organCode = '" + o.getOrganCode().toString() + "'";
		
		// 班组
		List<TOrgan> list = gzrwCreatePlanService.findListByHql(" from TOrgan where organNameEn like '%B' " + where  + " order by id");
		
		if ("0".equals(id)) {
			
			this.planbzList = new ArrayList<TPlanBz>();
			int i = 0;
			for (TOrgan organ : list) {
				TPlanBz bz = new TPlanBz();
				bz.setId(Long.parseLong("0"));
				bz.setDelflag("0");
				bz.setBzid(organ.getId());
				bz.setBzname(organ.getOrganName());
				this.planbzList.add(bz);
				i++;
			}
		} else {
			where = (o.getOrganCode().toString().length() <= 6 || "1".equals(super.getSession().getAttribute("SESSION_ISADMIN"))) ? "" : " and bzid  = '" + o.getId().toString() + "'";
			this.planbzList = gzrwCreatePlanService.findListByHql(" from TPlanBz where planid = '" + entity.getId() + "'" +  where  + " order by delflag");
		}
		
	}
	
	/**
	 * 进入详细页面
	 * 
	 * @return
	 * @throws AppException
	 */
	public String initDetail() throws AppException {
	       
		String   selectpage=super.getSearchBean().getCon1();

		// 修改进入
		if (!"0".equals(id)) {

			this.entity = (TGzrwdOld) gzrwCreatePlanService.findById(Long.parseLong(id),TPlan.class.getName());
			return SUCCESS;

		// 新建进入
		} else {
			
			this.entity = new TGzrwdOld();
			return SUCCESS;

		}
		
	}

	/**
	 * 保存、审核
	 * 
	 * @return
	 * @throws AppException
	 */
	public String save() throws AppException {
		
		String   selectpage=super.getSearchBean().getCon1();
		
		Date confDate = DateFormatTool.formatToDate(gzrwCreatePlanService.getSysdate("yyyy-MM-dd HH24:mi"), true);
		if (this.entity.getId() == 0) {
			
			this.entity.setOuid(super.getSession().getAttribute(IConstant.SESSION_TORGAN_CODE).toString());
		}
		String rtn = gzrwCreatePlanService.doSave(this.entity,this.hiddenids);
		
		super.setInfo(rtn);

		return SUCCESS;

	}
	
	/**
	 * -----提交计划----
	 * 
	 * @return
	 * @throws AppException
	 */
	public String submit() throws AppException {
		
		Date confDate = DateFormatTool.formatToDate(gzrwCreatePlanService.getSysdate("yyyy-MM-dd HH24:mi"), true);
		this.entity = (TGzrwdOld) gzrwCreatePlanService.findById(Long.parseLong(id),TGzrwdOld.class.getName());
		
		String rtn = gzrwCreatePlanService.doSubmit(this.entity,status);
		super.setInfo(rtn);
		if (status == "1"){
			return "SUBMIT_SUCCESS";
			
		}else {
			
			return "PASS_SUCCESS";
		}
	}

	public List<TBdz> getBdzList() {
		return bdzList;
	}

	public List<TDictionary> getDydjList() {
		return dydjList;
	}

	public List<TDictionary> getSblxList() {
		return sblxList;
	}

 

	public TGzrwdOld getEntity() {
		return entity;
	}

	public void setEntity(TGzrwdOld entity) {
		this.entity = entity;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public List<TPlanBz> getPlanbzList() {
		return planbzList;
	}

	public void setPlanbzList(List<TPlanBz> planbzList) {
		this.planbzList = planbzList;
	}

	public String getHiddenids() {
		return hiddenids;
	}

	public void setHiddenids(String hiddenids) {
		this.hiddenids = hiddenids;
	}


	public void setGzrwCreatePlanService(GzrwCreatePlanService gzrwCreatePlanService) {
		this.gzrwCreatePlanService = gzrwCreatePlanService;
	}

	public String getTdwf() {
		return tdwf;
	}

	public void setTdwf(String tdwf) {
		this.tdwf = tdwf;
	}

	public TPlan getPlan() {
		return plan;
	}

	public void setPlan(TPlan plan) {
		this.plan = plan;
	}
	
	


}
